package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle

import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.ActivityAgreementBinding
import com.haoshuang.zhouzhoubang.ui.adapter.MyPagerAdapter2
import com.haoshuang.zhouzhoubang.ui.fragment.WebFragment
import com.haoshuang.zhouzhoubang.ui.page.settled.autograph.AutographActivity
import com.haoshuang.zhouzhoubang.ui.page.settled.collaborationcosts.CollaborationCostsActivity
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.base.BaseViewModel
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *  
 */
class AgreementActivity : BaseVMActivity<BaseViewModel, ActivityAgreementBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = "签署协议"
            tvNext.clickWithTrigger{
                startPageForResult<AutographActivity>{code, data ->
                    if(code == 200){
                        val team = AccountUtils.user?.team
                        if (team?.is_pay_team_deposit == 1) {
                            // 进入费用页面
                            startPage<CollaborationCostsActivity>()
                        }
                        finish()
                    }
                }
            }
            tvCancle.clickWithTrigger{
               finish()
            }
        }
    }
    private val mTitles = arrayOf("入驻协议","服务保证","服务协议","安全守则","委托协议书")
    private val mFragments = ArrayList<Fragment>()
    private var mAdapter: MyPagerAdapter2? = null
    override fun initData() {
        for (index in mTitles.indices){
            val fm = WebFragment("${URLConstants.baseUrl}/index/index/agreement?id=${index+31}")
            mFragments.add(fm)
        }
        mAdapter = MyPagerAdapter2(supportFragmentManager,mFragments,mTitles)
        binding.vpOrder.adapter = mAdapter
        binding.slidingTabLayout.setViewPager( binding.vpOrder, mTitles)
        // 此处将其放置顶层即可，若是放到下面将会导致重复
        binding.vpOrder.offscreenPageLimit = mTitles.size
        binding.vpOrder.currentItem = 0  //设置初始化时显示的位置
        setOnPageChangeListener()
    }
    private fun setOnPageChangeListener() {
        binding.vpOrder.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }
    override fun startObserve() {
        LiveEventBus.get("gb", Boolean::class.java).observe(this@AgreementActivity) {
            finish()
        }
    }

}